package org.vt.ece4564.pcw.textinclass;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;
import android.util.Xml;

public class MessageXmlParser {
	
	public Map<String, List<String>> parse(String parseable) {
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			InputStream in = new ByteArrayInputStream(parseable.getBytes());
			parser.setInput(in, null);
			parser.nextTag();
			return readMessages(parser);
		} catch(Exception e) {
			Log.d("MessageXmlParserError", e.toString());
		}
		
		return null;
		
	}
	
	private Map<String, List<String>> readMessages(XmlPullParser parser) throws XmlPullParserException, IOException {
		Map<String, List<String>> messages = new HashMap<String, List<String>>();
		
		Log.d("XML", "here");
		
		parser.require(XmlPullParser.START_TAG, null, "messages");
		
		Log.d("XML", "there");
		
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			String name = parser.getName();
			
			if (name.equals("message")) {
				messages = readMessage(parser, messages);
			}
			else {
				skip(parser);
			}
		}
		
		return messages;
	}
	
	private Map<String, List<String>> readMessage(XmlPullParser parser, Map<String, List<String>> messages) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "message");
		String fromNumber = null;
		String message = null;
		List<String> messagesFromNumber = null;
		
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			
			String name = parser.getName();
			
			if (name.equals("number")) {
				fromNumber = readFromNumber(parser);
			}
			else if (name.equals("message-text")) {
				message = readFromMessage(parser);
			}
		}
		
		messagesFromNumber = messages.get(fromNumber);
		
		if (messagesFromNumber == null) {
			messagesFromNumber = new ArrayList<String>();
		}
		
		Log.d("numMessage", fromNumber + ":" + message);
		
		messagesFromNumber.add(message);
		
		messages.put(fromNumber, messagesFromNumber);
		
		return messages;
	}
	
	private String readFromNumber(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "number");
		String number = readText(parser);
		parser.require(XmlPullParser.END_TAG, null, "number");
		return number;
	}
	
	private String readFromMessage(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "message-text");
		String message = readText(parser);
		parser.require(XmlPullParser.END_TAG, null, "message-text");
		return message;
	}
	
	private String readText(XmlPullParser parser) throws XmlPullParserException, IOException {
		String result = "";
		
		if (parser.next() == XmlPullParser.TEXT) {
			result = parser.getText();
			parser.nextTag();
		}
		
		return result;
	}
	
	private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
		if (parser.getEventType() != XmlPullParser.START_TAG) {
			throw new IllegalStateException();
		}
		
		int depth = 1;
		
		while (depth != 0) {
			switch(parser.next()) {
				case XmlPullParser.END_TAG:
					depth--;
					break;
				case XmlPullParser.START_TAG:
					depth++;
					break;
			}
		}
	}

}
